<?php 
if (isset($isget)){
    
}else{
    if (isset($_GET['at']) && isset($_GET['on'])){
        $lat=$_GET['at'];
        $lon=$_GET['on'];
    }else{
        $lat='39.91488908';
        $lon='116.40387397';
    }
}
$token='ceb86b6e8666bf7d8b0fab1e1a0a5185';
$Password='ee8c2ba84fe467abf49229bfbfc06510';
$url='http://coapi.moji.com/whapi/json/lbsweather';
$timestamp=time();
$key=md5($Password.$timestamp.$lat.$lon);
$url.='?timestamp='.$timestamp.'&token='.$token.'&key='.$key;
$url.='&lat='.$lat.'&lon='.$lon;
$data=json_decode(httpGet($url), true);
$pm25=$data['data']['aqi']['value'];
$taiyang=$data['data']['condition']['condition'];
$conditionId=$data['data']['condition']['conditionId'];
$tem=$data['data']['condition']['temp'];
$wuran='优';
if($pm25 < 51){
    $wuran='优';
}
if($pm25 > 50 && $pm25 < 101){
    $wuran='良';
}
if($pm25 > 100 && $pm25 < 151){
    $wuran='轻度污染';
}
if($pm25 > 151 && $pm25 < 201){
    $wuran='中度污染';
}
if($pm25 > 200 && $pm25 < 301){
    $wuran='重度污染';
}
if($pm25 > 300){
    $wuran='严重污染';
}
$tq='yin';
if($conditionId < 13){
    $tq='qing';
}
if($conditionId > 12 && $conditionId < 15){
    $tq='yin';
}
if($conditionId > 14 && $conditionId < 24){
    $tq='yu';
}
if($conditionId > 37 && $conditionId < 58){
    $tq='yu';
}
if($conditionId > 65 && $conditionId < 71){
    $tq='yu';
}
if($conditionId > 23 && $conditionId < 26){
    $tq='xue';
}
if($conditionId > 57 && $conditionId < 66){
    $tq='xue';
}
if($conditionId > 70 && $conditionId < 78){
    $tq='xue';
}
if($conditionId == 78){
    $tq='yu';
}

if($conditionId > 78 && $conditionId < 82){
    $tq='qing';
}
if($conditionId > 85){
    $tq='yu';
}

if (isset($isget)){
    echo json_encode(array('isnew'=>1,'tq'=>$tq,'tem'=>$tem,'ty'=>$taiyang,'pm'=>$pm25,'wr'=>$wuran));
}
if (isset($_GET['op'])){
    $expire_time = time() + 3000;
    $putData=json_encode(array('tq'=>$tq,'tem'=>$tem,'ty'=>$taiyang,'pm'=>$pm25,'wr'=>$wuran,'lat'=>$lat,'lon'=>$lon,'expire_time'=>$expire_time));
    file_put_contents('./cache/weather/'.$_GET['op'].'.txt', $putData);
    
    function get_real_ip(){
        $ip=FALSE;
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
            for ($i = 0; $i < count($ips); $i++) {
                if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
                    $ip = $ips[$i];
                    break;
                }   
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }
    include 'config.php';
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    if (!$mysqli->set_charset("utf8")) {}
    $ip='';
    $ip=get_real_ip();
    $mysqli->query("insert into user_log(openid,ip,createtime) values('".$_GET['op']."','".$ip."','".date("Y-m-d H:i:s")."')  ");
    $mysqli->close();
}




function httpGet($url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}